package custom.security.web.async;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Callable;

/**
 * 异步rest服务提高服务器吞吐量
 * Created by security on 2017/9/24.
 */
@Slf4j
@RestController
public class AsyncController {

    @GetMapping("/get")
    public String get(){
        log.info("主线程开始");
        try {
            log.info("执行业务中......");
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("主线程完毕");
        return "success";
    }

    /**
     * 使用Callable开启一条副线程
     * @return
     */
    @GetMapping("/async/get")
    public Callable<String> asyncGet(){
        log.info("主线程开始");
        Callable<String> result = new Callable<String>() {

            @Override
            public String call() throws Exception {
                log.info("开启一条副线程");
                log.info("真正业务逻辑开始执行");
                Thread.sleep(1000);
                log.info("副线程执行完毕。");
                return "success";
            }
        };
        log.info("主线程完毕");
        return result;
    }

}
